extends Control


# Declare member variables here. Examples:
# var a = 2
# var b = "text"

onready var utils = load(data.infoDs["VEUtils"]["VEUtils"].dir).new()
onready var box = $ScrollContainer/GridContainer
onready var container = $ScrollContainer

onready var opt1 = $"../../OptBtns/VBoxContainer/Opt1"
onready var opt4 = $"../../OptBtns/VBoxContainer/Opt4"
onready var findBtn = $"../../OptBtns/VBoxContainer/findBtn"
onready var chaNum = $"../../OptBtns/VBoxContainer/chaNum"
# Called when the node enters the scene tree for the first time.
var isShow = false
var tag1List = []
var tag2List = []
var tag3List = []
var tag4List = []

var selectTag1 = null
var selectTag2 = null
var selectTag3= null
var selectTag4 = null
var findName

#子類全改了，原版的就先留著了
func init():
	if isShow : 
		reInit()
		return
	box.set_columns(8)
	tag1List = []
	isShow = true
	if name == "单位" :
		for i in data.getList("c") :
			var item = data.newBase(i.id)
			if item.lock == -1 :continue
			tag1List.append(item.getOrientation())
			var bt = preload("res://tscn/chara/charaItem.tscn").instance()
			bt.get_node("popLv").hide()
			bt.get_node("name").hide()
			addItem(item,bt)
	elif name == "技能" :
		for i in data.getList("k") :
			var item = data.newBase(i.id)
			if item.lock == -1 :continue
			var bt = preload("res://tscn/charaDlg/skill/skillBtn.tscn").instance()
			addItem(item,bt)
			bt.initTip()
			
	elif name == "物品" :
		for i in data.getList("csb") :
			var item = data.newBase(i.id)
			if item.lock == -1 :continue
			var bt = preload("res://tscn/item/itemBtn.tscn").instance()
			addItem(item,bt)
		for i in data.getList("gem") :
			var item = data.newBase(i.id)
			if item.lock == -1 :continue
			if item.isUnique == true :
				item.lv = 4
			var bt = preload("res://tscn/item/itemBtn.tscn").instance()
			addItem(item,bt)
		for i in data.getList("eqp") :
			var item = data.newBase(i.id)
			if item.lock == -1 :continue
			var bt = preload("res://tscn/item/itemBtn.tscn").instance()
			addItem(item,bt)
	elif name == "设施" :
		for i in data.getList("faci") :
			var item = data.newBase(i.id)
			if item.lock == -1 :continue
			if item.id.split("_").size() > 2 : continue
			var bt = preload("res://tscn/book/faciBtn.tscn").instance()
			addItem(item,bt)
			bt.init(item)
	elif name == "神徽" :
		for i in data.getList("r") :
			var item = data.newBase(i.id)
			if item.lock == -1 :continue
			var bt = preload("res://tscn/book/faciBtn.tscn").instance()
			addItem(item,bt)
			bt.init(item)

	

func addItem(item,bt):
	box.add_child(bt)
	bt.init(item)
	
	var modName = utils.getModNameFromData(item)
	if not tag1List.has(modName):
		tag1List.append(modName)
	
	if item.lock == 1 or not sys.main.ui.has_node("bookDlg"): return
	var lbt = preload("res://tscn/book/lock.tscn").instance()
	bt.add_child(lbt)
	lbt.init(item)
	lbt.position = Vector2(bt.rect_size.x * 0.5,bt.rect_size.y - 16)
	
	
	#lbt.init(item)
	
	
#override
func initOptBtns():
	pass

func _disconnect(opt):
	var connectArray = opt.get_signal_connection_list('onSelect')
	for sig in connectArray :
		opt.disconnect(sig.signal,sig.target,sig.method)
	
	connectArray = opt.get_signal_connection_list('onReset')
	for sig in connectArray :
		opt.disconnect(sig.signal,sig.target,sig.method)

func _initOptBtns():
	_disconnect(opt1)
	_disconnect(opt4)
	_disconnect(findBtn)
	
	
	opt1.set_visible(false)
	opt4.set_visible(false)
	findBtn.clearText()
	
	opt1.connect("onSelect",self,"r1")
	opt4.connect("onSelect",self,"r4")
	opt1.connect("onReset",self,"set",["selectTag1",null])
	opt4.connect("onReset",self,"set",["selectTag4",null])
	
	
	findBtn.connect("onSelect",self,"findByName")
	initOptBtns()

func _on_tab_draw():
	init()
	_initOptBtns()
	
func _ready():
	pass
	
	
func r1(tag):
	if tag1List.has(tag):
		selectTag1 = tag
	else:
		selectTag1 = null
	_reInit()
	

func r2(tag):
	if tag2List.has(tag):
		selectTag2 = tag
	else:
		selectTag2 = null
	_reInit()
	

func r3(tag):
	if tag3List.has(tag):
		selectTag3 = tag
	else:
		selectTag3 = null
	_reInit()
	

func r4(tag):
	if tag4List.has(tag):
		selectTag4 = tag
	else:
		selectTag4 = null
	_reInit()

func findByName(name):
	findName = name
	_reInit()
	
func reInit():
	pass
	
func _reInit():
	reInit()
	findName = null
	
	

func SetAllDisable(bl) :
	for i in box.get_children():
		if i.has_node("lock") and i.is_visible() :
			i.get_node("lock").setDisable(true)
